package cn.sciento.transfer.infra.datasource;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.AbstractDataSource;

public abstract class AbstractRoutingDataSource extends AbstractDataSource {
  protected abstract DataSource determineDataSource();
  
  public Connection getConnection() throws SQLException {
    return determineDataSource().getConnection();
  }
  
  public Connection getConnection(String username, String password) throws SQLException {
    return determineDataSource().getConnection(username, password);
  }
  
  public <T> T unwrap(Class<T> iface) throws SQLException {
    if (iface.isInstance(this)) {
      return (T)this;
    }
    return determineDataSource().unwrap(iface);
  }
  
  public boolean isWrapperFor(Class<?> iface) throws SQLException {
    return (iface.isInstance(this) || determineDataSource().isWrapperFor(iface));
  }
}
